/**
 * slide
 * Date: 15-7-24
 * Time: 上午11:40
 * To change this template use File | Settings | File Templates.
 */
(function($){
    $.extend({
        slideMove:function(container,flag){
            if(!container) return ;
            var element = container.children[0];
            var width,length;
            width = parseInt(element.children[0].offsetWidth);
            length = element.children.length;
            var allLength = width*length;
            element.style.width = allLength +"px";
            var diff = container.offsetWidth - width*length ;
            if(diff>=0) return;
            var start = {}; //初始的点
            var delta = {}; //移动的距离
            var startLeft = 0; //开始移动时的left


            function move(){
                var style = element.style;
                if((startLeft + delta.x) >= 0 && delta.x > 0) {
                    element.style.left = "0px";
                    return;
                }    //不能再往左划了
                if((startLeft + delta.x) <= diff && delta.x < 0)  {
                    element.style.left = diff +"px";
                    return;
                } //不能再往右划了
                element.style.left = (startLeft + delta.x)+"px";
            }
            var events = {

                handleEvent: function(event) {

                    switch (event.type) {
                        case 'touchstart': this.start(event); break;
                        case 'touchmove': this.move(event); break;
                        case 'touchend': this.end(event); break;
                    }
                },
                start: function(event) {
                    var touches = event.touches[0];
                    // 测量的起始值
                    start = {
                        // 得到初始的触摸坐标
                        x: touches.pageX,
                        y: touches.pageY,
                        time:+ new Date()
                    };
                    startLeft = parseInt(element.style.left);
                    container.addEventListener('touchmove', events, false);
                    container.addEventListener('touchend', events, false);
                },
                move: function(event) {
                    if ( event.touches.length > 1 || event.scale && event.scale !== 1) return ;
                    event.preventDefault();
                    var touches = event.touches[0];
                    delta = {
                        x:touches.pageX - start.x,
                        y:touches.pageY - start.y
                    };
                    var x = Math.abs(delta.x);
                    var y = Math.abs(delta.y);
                    if(x > y && y<90){
                        move();
                    }
                },
                end: function(event) {
                    var direction = delta.x < 0?-1:1; ///向左移
                    container.removeEventListener("touchmove",events,false);
                    container.removeEventListener("toucend",events,false);
                }
            }
            // 设置touchstart事件元素
            container.addEventListener('touchstart', events, false);//
            function autoPlace(){
                var half_length = (container.offsetWidth - width)/2;
                $(element).on("click","li",function(e){
                    var f_length = e.target.parentElement.offsetLeft,
                        b_length = allLength - f_length;
                    if(f_length >= half_length && b_length >= half_length){
                        element.style.left = (half_length - f_length )+"px";
                    }else if(f_length < half_length){
                        element.style.left = 0;
                    }else if( b_length < half_length){
                        element.style.left = diff +"px";
                    }
                });
            }
            if(flag) autoPlace();
        }
    });
})(jQuery)


